package com.qf.give_a_like.controller;

import com.qf.give_a_like.domain.Crad;
import com.qf.give_a_like.domain.User;
import com.qf.give_a_like.response.Response;
import com.qf.give_a_like.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@RestController
@RequestMapping("/user")
@SessionAttributes("{user}")
public class UserController {

    @Autowired
    UserService userService;

    //登录
    @RequestMapping("login")
    public Response<User> login(User user, String code, Model model, HttpServletRequest request){

        String captcha = (String) request.getSession().getAttribute("captcha");
        if (Objects.equals(captcha,code)){
            User login = userService.login(user.getUsername(), user.getPassword());
            if (Objects.nonNull(login)){
                model.addAttribute("user",login);
                return Response.success(login);
            }else {
                return Response.fail(-1,"用户名或密码有误",null);
            }
        }else {
            return Response.fail(-1,"验证码有误",null);
        }
    }


    //喜欢或不喜欢
    @GetMapping("like")
    public Response<User> likeCard(Integer userId,Integer card,Boolean like){
        User user = userService.likeCard(userId, card, like);
        return Response.success(user);
    }


}
